<template>
	<view class="order-submission">
		<template v-if="isShowMoney">
			<view class="mode-box-title">充值</view>
			<view class="balance">当前账户余额：{{ nowMoney }}</view>
		</template>
		<view class="mode-box">
		<view style="padding: 0 30rpx;">
			
			<view class="rechargeAmount">
				<van-row>
				  <van-col v-for="(item,key) in options" :key="key" span="8">
					  <view @click="onSelect(item.chargeAmount,key)" class="amount" :class="{curr:index==key}">&yen;{{ item.chargeAmount }}
						<view v-if="item.sendAmount > 0 && active == 'weixin'" class="send-money">多送{{ item.sendAmount }}</view>
					  </view>
				  </van-col>
				</van-row>
			</view>
			<input class="extract_input" @blur="onBlur" maxlength="6" type="text" placeholder="请输入充值金额" :value="amount" />
		</view>
		<div class="wrapper">
			<div class="item">
				<div>支付方式</div>
				<div class="list">
					<div class="payItem acea-row row-middle" :class="active === 'weixin' ? 'on' : ''"
						@click="payItem('weixin')">
						<div class="name acea-row row-center-wrapper">
							<div class="iconfont icon-weixin2" :class="active === 'weixin' ? 'bounceIn' : ''"></div>微信支付
						</div>
						<div class="tip">微信快捷支付</div>
					</div>
					<div class="payItem acea-row row-middle" :class="active === 'now_money' ? 'on' : ''"
						@click="payItem('now_money')">
						<div class="name acea-row row-center-wrapper">
							<div class="iconfont icon-icon-test" :class="active === 'now_money' ? 'bounceIn' : ''"></div>余额支付
						</div>
						<div class="tip">余额：{{ nowMoney || 0 }}</div>
					</div>
				</div>
			</div>
		</div>
		<view v-if="isShowMoney" class="button_box" >
			<button class="extract_button" type="default" @click="toPay">{{ amount?`￥${amount}`:'' }} 确认支付</button>
		</view>
		<view v-if="!isShowMoney" class="button_box button_box-fixed acea-row row-middle row-center">
			<button class="extract_button extract_button-fixed" type="default" @click="toPay">{{ amount?`￥${amount}`:'' }} <text>确认支付</text></button>
		</view>
	</view>
	</view>
	
</template>

<script>
	import { weappPay } from "@/libs/wechat.js";
	import { addcoins, getCoinsSetting } from "@/api/user";
	export default {
		props:{
			nowMoney: Number,
			isShowMoney: Boolean
		},
		data() {
			return {
				amount: 0,
				value:'',
				index:0,
				options:[],
				active: "weixin",
			}
		},
		mounted() {
			this.initData();
		},
		methods: {
			initData() {
				getCoinsSetting().then(res => {
					this.options = res.data || [];
					if (this.options.length > 0) {
						this.onSelect(this.options[0].chargeAmount, 0);
					}
				})
			},
			onBlur(e){
				var val =e.detail.value;
				if(Number(val)>0)  this.amount = Number(val).toFixed(2);
			},
			onSelect(val,key){
				this.index = key
				this.amount = Number(val).toFixed(2)
			},
			payItem: function(index) {
				this.active = index;
			},
			// 立即充值
			toPay(){
				let that = this;
				if(!this.amount>0){
					return uni.showToast({
						title: "充值金额错误",
						icon: "none",
						duration: 2000
					});
				}
				if (this.active == 'now_money' && this.nowMoney < this.amount) {
					return uni.showToast({
						title: "余额不足",
						icon: "none",
						duration: 2000
					});
				}
				addcoins({price:this.amount, payType: this.active}).then(res=>{
					let data = res.data;
					if (this.active == 'now_money') {
						wx.showToast({
							title: "充值成功",
							icon: "success",
							duration: 2000
						});
						// 支付完更新用户信息
						setTimeout(function() {
							that.$emit('onRefresh', true)
						}, 2000)
					} else {
						// 调用微信支付
						weappPay(data.result.jsConfig).then(resPay=>{
							wx.showLoading({
								mask: true,
								title: '支付结果确认中，请稍后',
							});
							//支付成功后立即点完成会跳转到待付款页面，故添加延时
							setTimeout(function() {
								wx.hideLoading();
								that.$emit('onRefresh', true)
							}, 3000);
						}).catch(err=>{
							wx.hideLoading();
							uni.showToast({
								title:err.msg,
								icon:"none",
								duration:2000
							});
						})
					}
				}).catch(error=>{
					wx.hideLoading();
					uni.showToast({
						title: error.msg,
						icon:"none",
						duration:2000
						
					});
				})
			},
		}
	}
</script>

<style scoped lang="less">
	.mode-box-title{
		font-size: 36rpx;
		font-weight: 500;
		color: #000000;
		text-align: center;
		border-bottom: 1px solid #F4F4F4;
		padding-bottom: 12rpx;
		margin-bottom: 33rpx;
	}
	.balance{
		font-size: 26rpx;
		color: #221815;
		padding-left: 30rpx;
	}
	.send-money {
		position: relative;
		bottom: 90rpx;
		font-size: 28rpx;
	}
	.mode-box {
		// min-height: 100vh;
		background: #FFFFFF;
		position: relative;
		// padding: 0 40rpx;
		box-sizing: border-box;
		
		.rechargeAmount{
			margin:0 -5px;
			padding:20px 0;
			.amount{
				&.curr{
					border-color:#BD4044;
					color:#BD4044;
				}
				text-align: center;
				border:2px solid #898989;
				color:#898989;
				margin:5px;
				height:129rpx;
				line-height: 129rpx;
				border-radius: 10rpx
			}
		}

		.instruction {
			position: absolute;
			top: 0;
			right: 10rpx;
			padding: 0 30rpx;
			font-size: 24rpx;
			color: #515151;
			line-height: 60rpx;
		}

		.available_balance {
			padding-top: 60rpx;
			line-height: 60rpx;
			color: #000000;
			font-size: 24rpx;
		}

		.extract_input {
			height: 100rpx;
			border: 1rpx solid #E8E8E8;
			border-radius: 10rpx;
			border: 1rpx solid #E8E8E8;
			color: #000000;
			font-size: 50rpx;
			font-weight: bold;
			text-align: center;
		}

		.expected_arrival {
			line-height: 60rpx;
			color: #000000;
			font-size: 22rpx;
		}

		.extract_details {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			font-size: 22rpx;
			color: #515151;

			.details_left {
				margin-right: 50rpx;
			}
		}

		.button_box {
			display: flex;
			justify-content: center;
			align-items: center;
			margin-top: 200rpx;

			.extract_button {
				width: 393rpx;
				height: 70rpx;
				line-height: 70rpx;
				background: #BD4044;
				border-radius: 35rpx;
				color: #FFFFFF;
				font-size: 26rpx;
			}
			.extract_button-fixed{
				width: 460rpx;
				height: 72rpx;
				line-height: 72rpx;
				border-radius: 36rpx;
				font-size: 32rpx;
				position: fixed;
				bottom: 14rpx;
				text{
					margin-left: 40rpx;
				}
			}
		}
		.button_box-fixed{
			width: 748rpx;
			height: 98rpx;
			background-color: #fff;
			position: fixed;
			bottom: 0;
			left: 0;
			.extract_button-fixed{
				width: 460rpx;
				height: 72rpx;
				line-height: 72rpx;
				border-radius: 36rpx;
				font-size: 32rpx;
				text{
					margin-left: 40rpx;
				}
			}
		}
	}

	.spacing {
		margin-top: 30rpx;
		height: 20rpx;
		background-color: #F9F9F9;
	}

	.withdraw_address {
		padding: 6rpx 30rpx;

		.bankCard {
			height: 80rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.bank {
				display: flex;
				align-items: center;
				flex-wrap: nowrap;
			}

			.choose_card {
				display: flex;
				color: #999999;
				font-size: 24rpx;
				justify-content: center;
				align-items: center;

				.icon {
					margin-left: 30rpx;
				}
			}
		}

		.pocket_money {
			height: 80rpx;
			display: flex;
			align-items: center;
			flex-wrap: nowrap;
		}

		.img_icon {
			margin: 0 20rpx;
			height: 54rpx;
			width: 54rpx;
		}

		.explanation {
			font-size: 28rpx;
			color: #666666;
		}

		.select {
			height: 36rpx;
			width: 36rpx;
			border-radius: 20rpx;

			.weixuan {
				font-size: 36rpx;
				color: #999999;
			}

			.yixuan {
				font-size: 36rpx;
				color: #FF563D;
			}
		}
	}
</style>
